//
//  ByViewShopCarStoreHeader.m
//  mijifen_ios
//
//  Created by 朱凯 on 2017/10/26.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByViewShopCarStoreHeader.h"

@implementation ByViewShopCarStoreHeader
{
    UIButton    *_selectBtn;            // 选中视图
    UIImageView *_iconView;             // 主图
    UIView      *_sepLine;              // 两个cell之间的分割
    UILabel     *_titleLabel;           // 标题
    UIView      *_tagsView;             // 标签视图
    UIImageView *_arrowView;            // 箭头

}
- (instancetype)init {
    
    if (self = [super init]) {
        
        self.backgroundColor = [UIColor whiteColor];
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    
    // 选中视图
    _selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_selectBtn setImage:[UIImage imageNamed:@"购物车未选中"] forState:UIControlStateNormal];
    [_selectBtn setImage:[UIImage imageNamed:@"购物车选中"] forState:UIControlStateSelected];
    [_selectBtn addTarget:self action:@selector(select:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_selectBtn];
    
    // icon
    _iconView = [[UIImageView alloc] init];
    _iconView.backgroundColor = BY_BLUE_COLOR;
    _iconView.layer.cornerRadius = 15;
    _iconView.layer.masksToBounds = true;
    [self addSubview:_iconView];
    
    // 分割
    _sepLine = [[UIView alloc] init];
    _sepLine.backgroundColor = BY_LINE_COLOR;
    [self addSubview:_sepLine];
    
    // 标题
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.numberOfLines = 2;
    _titleLabel.font = FONT(14);
    _titleLabel.text = @"耐克旗舰店";
    _titleLabel.textColor = [UIColor blackColor];
    _titleLabel.textAlignment = NSTextAlignmentLeft;
    [self addSubview:_titleLabel];
    
    // 标签
    _tagsView = [UIView new];
    [self addSubview:_tagsView];
    
    // 箭头
    _arrowView = [[UIImageView alloc] init];
    _arrowView.image = [UIImage imageNamed:@"右箭头"];
    [self addSubview:_arrowView];
    
    
    // 布局
    [_selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(@0);
        make.width.equalTo(@30);
    }];
    [_iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_selectBtn.mas_right);
        make.size.mas_equalTo(CGSizeMake(30, 30));
        make.centerY.equalTo(@0);
    }];
    [_sepLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(@0);
        make.height.equalTo(@1);
    }];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_iconView.mas_right).offset(10);
        make.top.bottom.equalTo(_iconView);
    }];
    [_arrowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(20, 20));
        make.centerY.equalTo(@0);
        make.left.equalTo(_tagsView.mas_right).offset(0);
    }];
    

}

- (void)setModel:(ByModelShopCarStore *)model {
    
    _model = model;
    [self setNeedsLayout];
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    _selectBtn.selected = _model.selected;
    
    _titleLabel.text = _model.name;
    
    [_selectBtn setImageEdgeInsets:UIEdgeInsetsMake((self.height - 20) / 2, 5, (self.height - 20) / 2, 5)];
    
    // 动态创建标签视图
    [_tagsView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    _tagsView.left = _titleLabel.right + 5;
    _tagsView.height = 20;
    _tagsView.y = self.height / 2;
    CGFloat x = 0, y, w, h;
    for (int i = 0; i < _model.tags.count; i ++) {
        y = 0;
        w = [_model.tags[i] getSizeOfString:12].width + 10;
        h = _tagsView.height;
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, w, h)];
        label.text = _model.tags[i];
        label.font = FONT(12);
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = BY_RED_COLOR;
        label.layer.cornerRadius = 4;
        label.layer.masksToBounds = true;
        label.layer.borderColor = BY_RED_COLOR.CGColor;
        label.layer.borderWidth = 1;
        [_tagsView addSubview:label];
        
        x += w + 5;
    }
    _tagsView.width = _tagsView.subviews.lastObject.right;
}


- (void)select:(UIButton *)button {
    
    button.selected = !button.selected;
    
    if (self.selectBlock) {
        self.selectBlock(button.selected);
    }
}

@end
